/*
package com.wqm.step.controller;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.junit.Test;

import com.wqm.step.common.utils.HibernateUtils;
import com.wqm.step.pojo.Customer;
import com.wqm.step.pojo.Order;


*/
/**
 *  纯粹的面向对象查询方法
 * @author wangqinmin
 * @好诗： 青山不改，绿水长流
 *//*

public class QBCTest {
	// 离线的检索
	@Test
	public void test6() {
		// 1.得到一个DetachedCriteria
		DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
		dc.add(Restrictions.like("name", "张_"));

		// 2.生成Criteria执行操作
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		Criteria criteria = dc.getExecutableCriteria(session);
		List<Customer> list = criteria.list();

		System.out.println(list);
		session.getTransaction().commit();
		session.close();

	}

	// 统计检索
	@Test
	public void test5() {
		Session session = HibernateUtils.openSession();
		session.beginTransaction();

		// 1.统计订单总数
		Criteria criteria = session.createCriteria(Order.class);
		// Object obj =
		// criteria.setProjection(Projections.rowCount()).uniqueResult();
		// //统计总行数 count(id)
		// System.out.println(obj);

		// 2.订单的总价格----分组统计根据客户
		// criteria.setProjection(Projections.sum("money")); //统计总金额

		criteria.setProjection(
				Projections.projectionList().add(Projections.sum("money")).add(Projections.groupProperty("c")));

		List<Object[]> list = criteria.list(); // 在这个集合中保存的是Object[money的统计信息,客户信息]

		for (Object[] objs : list) {
			for (Object obj : objs) {
				System.out.println(obj);
			}
		}

		session.getTransaction().commit();
		session.close();
	}

	// 分页检索
	@Test
	public void test4() {
		Session session = HibernateUtils.openSession();
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Order.class);
		criteria.setFirstResult((2 - 1) * 6);
		criteria.setMaxResults(6);
		List<Order> list = criteria.list();

		System.out.println(list);

		session.getTransaction().commit();
		session.close();
	}

	// 条件检索
	@Test
	public void test3() {
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		// 1.查询名称叫张某客户 张_
		Criteria criteria = session.createCriteria(Customer.class);
		Criterion like = Restrictions.like("name", "张_"); // 其它的条件 lt < gt > le
															// <= ge>= eq==
		criteria.add(like);// 添加条件
		Customer c = (Customer) criteria.uniqueResult();
		System.out.println(c);

		// 2.查询订单价格在1050以上的，并且它的客户是张某
		Criteria cri = session.createCriteria(Order.class);
		SimpleExpression lt = Restrictions.gt("money", 1050d); // >1050
		SimpleExpression eq = Restrictions.eq("c", c); // 与c客户相同
		LogicalExpression and = Restrictions.and(lt, eq);
		cri.add(and);
		// List<Order> orders =
		// cri.add(Restrictions.and(Restrictions.gt("money", 1050d),
		// Restrictions.eq("c", c))).list();
		List<Order> orders = cri.list();
		System.out.println(orders);

		session.getTransaction().commit();
		session.close();

	}

	// 排序检索
	@Test
	public void test2() {
		// 查询订单信息 根据订单的价格进行排序
		Session session = HibernateUtils.openSession();
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Order.class);
		// 指定排序
		// criteria.addOrder(org.hibernate.criterion.Order.desc("money")); // 降序
		criteria.addOrder(org.hibernate.criterion.Order.asc("money")); // 升序

		List<Order> list = criteria.list();

		System.out.println(list);

		session.getTransaction().commit();
		session.close();
	}

	// 基本检索
	@Test
	public void test1() {
		// 查询所有Customer
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		// 1.得到一个Criteria对象
		Criteria criteria = session.createCriteria(Customer.class);

		// 2.调用list方法
		List<Customer> list = criteria.list();

		System.out.println(list);

		session.getTransaction().commit();
		session.close();
	}
}
*/
